package com.smartian.v2.impl;

import android.text.TextUtils;

import com.smartian.v2.AbstractTextParser;
import com.smartian.v2.OpLog;
import com.smartian.v2.TextPattern;
import com.smartian.v2.filters.TextParserInterceptorManager;
import com.smartian.v2.model.DateTimeNode;
import com.smartian.v2.model.ScheduleTaskBean;
import com.smartian.v2.utils.DateTimeHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by tianweitong on 2020/3/13.
 */

public class WeeklyTextParser extends AbstractTextParser {
    private final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
    private final List<DateTimeNode> mDateTimeNodeList = new ArrayList<>();
    private int mFrequent;
    private volatile boolean mHasParsed = false;

    public WeeklyTextParser(String sourceText, String repeatText, int repeatTextIndex) {
        super(sourceText, repeatText, repeatTextIndex);
    }

    @Override
    public ScheduleTaskBean getScheduleTaskBean() {
        startHandler();
        OpLog.d("TextParser", mDateTimeNodeList + " , " + mFrequent);
        if (mDateTimeNodeList != null) {
            Collections.sort(mDateTimeNodeList);
        }

        DateTimeNode dateNode = DateTimeHelper.findBestDateNode(mDateTimeNodeList, DateTimeNode.TYPE_DATE);
        DateTimeNode timeNode = DateTimeHelper.findBestDateNode(mDateTimeNodeList, DateTimeNode.TYPE_TIME);

        if (dateNode == null || timeNode == null) {
            return null;
        }
        if (TextUtils.isEmpty(dateNode.resultText) || TextUtils.isEmpty(timeNode.resultText)) {
            return null;
        }

        List<DateTimeNode> dateTimeNodes = new ArrayList<>();

        dateTimeNodes.add(dateNode);
        dateTimeNodes.add(timeNode);

        String date = dateNode.resultText;
        String time = timeNode.resultText;

        String dateTime = DateTimeHelper.formatWeekTime(date, time);

        ScheduleTaskBean scheduleTaskBean = new ScheduleTaskBean();
        scheduleTaskBean.setFraquent(mFrequent);
        scheduleTaskBean.setRepeatType(TextPattern.REPEAT_WEELY);
        scheduleTaskBean.setRepeatText(getRepeatText());
        scheduleTaskBean.setDateTime(dateTime);
        scheduleTaskBean.setDateTimeFormat(getDateTimeFormat());
        scheduleTaskBean.setSourceText(getSourceText());
        scheduleTaskBean.setDateTimeNodes(dateTimeNodes);

        return scheduleTaskBean;
    }

    private List<DateTimeNode> startParse() {


        List<DateTimeNode> dateTimeNodes = new ArrayList<>();
        String sourceText = getSourceText();

        if (TextUtils.isEmpty(sourceText)) {
            mFrequent = -1;
            return dateTimeNodes;
        }

        mFrequent = parseFrequent();
        if (mFrequent < 0) return dateTimeNodes;

        List<DateTimeNode> dateNodes = getParseDate();
        List<DateTimeNode> timeNodes = getParseTime();

        if (dateNodes.size() == 0) {

            DateTimeNode dateNode = new DateTimeNode();
            dateNode.text = "";
            dateNode.index = 0;
            dateNode.resultText = DateTimeHelper.getToday();
            dateNode.locatorText = "";
            dateNode.type = DateTimeNode.TYPE_DATE;

            dateNodes.add(dateNode);
        }

        if (timeNodes.size() == 0) {
            String currentTime = DateTimeHelper.getCurrentTime();
            DateTimeNode dateTimeNode = new DateTimeNode();
            dateTimeNode.text = "";
            dateTimeNode.index = 0;
            dateTimeNode.resultText = currentTime;
            dateTimeNode.locatorText = "";
            dateTimeNode.type = DateTimeNode.TYPE_TIME;
            timeNodes.add(dateTimeNode);
        }

        if (dateNodes != null) {
            dateTimeNodes.addAll(dateNodes);
        }
        if (timeNodes != null) {
            dateTimeNodes.addAll(timeNodes);
        }

        if (dateTimeNodes.size() > 0) {
            Collections.sort(dateTimeNodes);
        }
        return dateTimeNodes;

    }


    private void startHandler() {
        if (!mHasParsed) {
            List<DateTimeNode> dateTimeNodes = startParse();
            if (dateTimeNodes != null) {
                mDateTimeNodeList.clear();
                dateTimeNodes = TextParserInterceptorManager.dispatchIntercept(this, TextPattern.REPEAT_WEELY, dateTimeNodes);
                mDateTimeNodeList.addAll(dateTimeNodes);
            }
            mHasParsed = true;
        }
    }

    @Override
    public boolean isValid() {
        startHandler();
        return mFrequent > 0;
    }

    @Override
    public String getDateTimeFormat() {
        return "E HH:mm";
    }
}
